package ar.unc.famaf.pln.giveselector.filters;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import ar.unc.famaf.pln.giveselector.classifier.IClassifier;
import ar.unc.famaf.pln.giveselector.classifier.UtteranceComparator;
import ar.unc.famaf.pln.giveselector.classifier.UtteranceP;
import ar.unc.famaf.pln.giveselector.context.Context;
import ar.unc.famaf.pln.giveselector.context.Utterance;

/**
 * Performance Filter. This class filters all the given utterances that
 * have negative score. The score for each Utterance is computed using 
 * the given classifier object in the constructor.
 * 
 * @author David Racca ( david.racca@gmail.com )
 *
 */
public class PFilter implements IFilter {

	private IClassifier classifier;
	private Comparator<Utterance> comparator;

	/**
	 * Creates a new Performace Filter object.
	 */
	public PFilter(IClassifier classifier)
	{
		this.classifier = classifier;
		this.comparator = Collections.reverseOrder(new UtteranceComparator(classifier));
	}

	/**
	 * @return all the Utterance objects that have positive performance score.
	 */
	@Override
	public Set<Utterance> filter(Set<Utterance> utterances, Context context) {
		List<Utterance> utts = new ArrayList<Utterance>();
		utts.addAll(utterances);
		Collections.sort(utts, comparator);

		Set<Utterance> result = new HashSet<Utterance>();

		if (!utts.isEmpty())
		{
		
			System.out.println("===== PERFORMANCE FILTER");
			boolean discarded = false;
			
			for (Utterance u : utts)
			{
				float score = classifier.getScore(u);
				if (score >= 0f)
				{
					result.add(u);
					discarded = false;
				}
				else
					discarded = true;
				System.out.println("Score: " + ((u instanceof UtteranceP) ? score : "n/i") 
						+ " [" + u.getId() + "] " + u.getText() + ((discarded) ? " | DISCARDED" : ""));
			}
			
			System.out.println("==========================================");
		}
		
		return result;
	}

}
